﻿using BST250301TestStand.Models.Utils;
using CsvHelper;
using NPOI.SS.Formula.Functions;
using System.Globalization;
using System.IO;

namespace BST250301TestStand.Models.Helpers
{
    internal class CSVH
    {
        // 读取整个CSV到列表，然后访问指定行列
        public static string GetCellValue(string filePath, int rowIndex, int columnIndex)
        {
            using var reader = new StreamReader(filePath);
            using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
            var result = csv.Read();
            int rows = 0;
            while (csv.Read())
            {
                rows++;
                string name = csv.GetField<string>(2);        // 第1列
            }
            return string.Empty;
        }
        public static void SaveListToCsv<T>(List<T> list, string filePath)
        {
            using (var writer = new StreamWriter(filePath))
            using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                csv.WriteRecords(list);
            }
        }
        public static void SaveListToCsvByName<T>(List<RecordData> list, string filePath)
        {
            using (var writer = new StreamWriter(filePath))
            using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                csv.WriteRecords(list.Select(x => new { 记录ID = x.ID, 方案名 = x.PlanName, 子方案名 = x.SubPlanName, 项目名称 = x.ProjectName, 产品SN = x.ProductSN, 项目日期 = x.ProjectDT, 参数名称 = x.ReportName, 参数标准值 = x.DefaultValue, 参数平均值 = x.ReadAVG, 参数最大值 = x.ReadMax, 参数最小值 = x.ReadMin, 评定结果 = x.QC }));
            }
        }
    }
}